<template>
  <div class="home">
    <div class="login-content">
      <div class="user-image"></div>
      <el-form :model="ruleForm" :rules="loginFormRules" ref="ruleForm">
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" prefix-icon="el-icon-user" placeholder="请输入用户名" clearable></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="ruleForm.password" placeholder="请输入密码" prefix-icon="el-icon-user" show-password></el-input>
        </el-form-item>
        <el-form-item class="btn-container">
          <el-button type="primary" @click="submitForm('ruleForm')">
            登录
          </el-button>
          <el-button @click="resetForm('ruleForm')">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts">
import { ElForm } from 'element-ui/types/form';
import Vue from 'vue';

export default Vue.extend({
  name: 'Home',
  data() {
    return {
      // 这是登录表单的数据绑定对象
      ruleForm: {
        username: '',
        password: '',
      },
      // 这是表单的验证规则对象
      loginFormRules: {
        // 验证用户名是否合法
        username: [
          {
            required: true,
            message: '请输入登录名称',
            trigger: 'blur',
          },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
        // 验证密码是否合法
        password: [
          {
            required: true,
            message: '请输入登录密码',
            trigger: 'blur',
          },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur',
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName: string) {
      let elform: ElForm = this.$refs[formName] as ElForm;
      elform.validate(valid => {
        console.log(valid);

        if (valid) {
          alert('submit!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName: string) {},
  },
});
</script>

<style lang="scss" scoped>
.home {
  position: relative;
  width: 100%;
  height: 100%;
  background-color: #1f4a69;

  .login-content {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 450px;
    height: 300px;
    background-color: #fcfefc;

    .user-image {
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 100px;
      height: 100px;
      border-radius: 50%;
      border: 8px white solid;
      box-shadow: 0px 0px 10px #888888;
      background-color: #eceeec;
      background-size: contain;
      background-image: url(~@/assets/logo.png);
    }
  }

  .el-form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
  .btn-container {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
